using System;
using System.Configuration;
using System.Web;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Web;
using DevExpress.ExpressApp.Web.TestScripts;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.AuditTrail;
using DevExpress.Web.ASPxClasses;

namespace XCRM.Web {
	public class Global : System.Web.HttpApplication {
        private static void webApplication_CustomizeFormattingCulture(object sender, CustomizeFormattingCultureEventArgs e) {
            e.FormattingCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
        }

		public Global() {
#if DEBUG
            DevExpress.EasyTest.Framework.EasyTestTracer.Tracer.SetTraceLevel(System.Diagnostics.TraceLevel.Verbose);
#endif
			InitializeComponent();
		}
		protected void Application_Start(object sender, EventArgs e) {
            ASPxWebControl.CallbackError += new EventHandler(Application_Error);
#if DEBUG
			TestScriptsManager.EasyTestEnabled = true;
#endif
		}
		protected void Session_Start(object sender, EventArgs e) {
			WebApplication.SetInstance(Session,
				new XCRM.Web.XCRMWebApplication());
            WebApplication.Instance.CustomizeFormattingCulture += new EventHandler<CustomizeFormattingCultureEventArgs>(webApplication_CustomizeFormattingCulture);
            WebApplication.Instance.ConnectionString = string.Format(@"XpoProvider=XmlDataSet;Data Source={0}App_Data\xcrm.xml", AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
            if(ConfigurationManager.AppSettings["SiteMode"] != null && ConfigurationManager.AppSettings["SiteMode"].ToLower() == "true") {
                WebApplication.Instance.ConnectionString = InMemoryDataStoreProvider.ConnectionString + string.Format(@";Data Source={0}App_Data\xcrm.xml", AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
            }
            else {
                if(ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {
                    WebApplication.Instance.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                }
            }

#if EasyTest
			WebApplication.Instance.ConnectionString = ConfigurationManager.ConnectionStrings["EasyTestConnectionString"].ConnectionString;
#endif

			WebApplication.Instance.Setup();
			WebApplication.Instance.Start();
		}

		private void Instance_QueryCurrentUserName(object sender, QueryCurrentUserNameEventArgs e) {
			e.CurrentUserName = String.Format("Web user ({0})", HttpContext.Current.Request.UserHostAddress);
		}

		protected void Application_BeginRequest(object sender, EventArgs e) {
			string filePath = HttpContext.Current.Request.PhysicalPath;
			if(!string.IsNullOrEmpty(filePath)
				&& (filePath.IndexOf("Images") >= 0) && !System.IO.File.Exists(filePath)) {
				HttpContext.Current.Response.End();
			}
		}
		protected void Application_EndRequest(object sender, EventArgs e) {
		}
		protected void Application_AuthenticateRequest(object sender, EventArgs e) {
		}
		protected void Application_Error(object sender, EventArgs e) {
			ErrorHandling.Instance.ProcessApplicationError();
		}
		protected void Session_End(object sender, EventArgs e) {
			WebApplication.DisposeInstance(Session);
		}
		protected void Application_End(object sender, EventArgs e) {
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {
		}
		#endregion
	}
}
